home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / bash1135 / lib / readline / doc / readline.inf < prev   
Encoding:
GNU Info File  |  1994-03-01  |  61.1 KB  |  1,709 lines

  1. This is Info file readline.info, produced by Makeinfo-1.55 from the
  2. input file rlman.texinfo.
  3.  
  4.    This document describes the GNU Readline Library, a utility which
  5. aids in the consistency of user interface across discrete programs that
  6. need to provide a command line interface.
  7.  
  8.    Copyright (C) 1988, 1991 Free Software Foundation, Inc.
  9.  
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice pare
  12. preserved on all copies.
  13.  
  14.    Permission is granted to copy and distribute modified versions of
  15. this manual under the conditions for verbatim copying, provided that
  16. the entire resulting derived work is distributed under the terms of a
  17. permission notice identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that this permission notice may be stated in a
  22. translation approved by the Foundation.
  23.  
  24. File: readline.info,  Node: Top,  Next: Command Line Editing,  Prev: (DIR),  Up: (DIR)
  25.  
  26. GNU Readline Library
  27. ********************
  28.  
  29.    This document describes the GNU Readline Library, a utility which
  30. aids in the consistency of user interface across discrete programs that
  31. need to provide a command line interface.
  32.  
  33. * Menu:
  34.  
  35. * Command Line Editing::       GNU Readline User's Manual.
  36. * Programming with GNU Readline::  GNU Readline Programmer's Manual.
  37. * Concept Index::           Index of concepts described in this manual.
  38. * Function and Variable Index::       Index of externally visible functions
  39.                    and variables.
  40.  
  41. File: readline.info,  Node: Command Line Editing,  Next: Programming with GNU Readline,  Prev: Top,  Up: Top
  42.  
  43. Command Line Editing
  44. ********************
  45.  
  46.    This text describes GNU's command line editing interface.
  47.  
  48. * Menu:
  49.  
  50. * Introduction and Notation::    Notation used in this text.
  51. * Readline Interaction::    The minimum set of commands for editing a line.
  52. * Readline Init File::        Customizing Readline from a user's view.
  53.  
  54. File: readline.info,  Node: Introduction and Notation,  Next: Readline Interaction,  Up: Command Line Editing
  55.  
  56. Introduction to Line Editing
  57. ============================
  58.  
  59.    The following paragraphs describe the notation we use to represent
  60. keystrokes.
  61.  
  62.    The text C-k is read as `Control-K' and describes the character
  63. produced when the Control key is depressed and the k key is struck.
  64.  
  65.    The text M-k is read as `Meta-K' and describes the character
  66. produced when the meta key (if you have one) is depressed, and the k
  67. key is struck.  If you do not have a meta key, the identical keystroke
  68. can be generated by typing ESC first, and then typing k.  Either
  69. process is known as "metafying" the k key.
  70.  
  71.    The text M-C-k is read as `Meta-Control-k' and describes the
  72. character produced by "metafying" C-k.
  73.  
  74.    In addition, several keys have their own names.  Specifically, DEL,
  75. ESC, LFD, SPC, RET, and TAB all stand for themselves when seen in this
  76. text, or in an init file (*note Readline Init File::., for more info).
  77.  
  78. File: readline.info,  Node: Readline Interaction,  Next: Readline Init File,  Prev: Introduction and Notation,  Up: Command Line Editing
  79.  
  80. Readline Interaction
  81. ====================
  82.  
  83.    Often during an interactive session you type in a long line of text,
  84. only to notice that the first word on the line is misspelled.  The
  85. Readline library gives you a set of commands for manipulating the text
  86. as you type it in, allowing you to just fix your typo, and not forcing
  87. you to retype the majority of the line.  Using these editing commands,
  88. you move the cursor to the place that needs correction, and delete or
  89. insert the text of the corrections.  Then, when you are satisfied with
  90. the line, you simply press RETURN.  You do not have to be at the end of
  91. the line to press RETURN; the entire line is accepted regardless of the
  92. location of the cursor within the line.
  93.  
  94. * Menu:
  95.  
  96. * Readline Bare Essentials::    The least you need to know about Readline.
  97. * Readline Movement Commands::    Moving about the input line.
  98. * Readline Killing Commands::    How to delete text, and how to get it back!
  99. * Readline Arguments::        Giving numeric arguments to commands.
  100.  
  101. File: readline.info,  Node: Readline Bare Essentials,  Next: Readline Movement Commands,  Up: Readline Interaction
  102.  
  103. Readline Bare Essentials
  104. ------------------------
  105.  
  106.    In order to enter characters into the line, simply type them.  The
  107. typed character appears where the cursor was, and then the cursor moves
  108. one space to the right.  If you mistype a character, you can use DEL to
  109. back up, and delete the mistyped character.
  110.  
  111.    Sometimes you may miss typing a character that you wanted to type,
  112. and not notice your error until you have typed several other
  113. characters.  In that case, you can type C-b to move the cursor to the
  114. left, and then correct your mistake.  Aftwerwards, you can move the
  115. cursor to the right with C-f.
  116.  
  117.    When you add text in the middle of a line, you will notice that
  118. characters to the right of the cursor get `pushed over' to make room
  119. for the text that you have inserted.  Likewise, when you delete text
  120. behind the cursor, characters to the right of the cursor get `pulled
  121. back' to fill in the blank space created by the removal of the text.  A
  122. list of the basic bare essentials for editing the text of an input line
  123. follows.
  124.  
  125. C-b
  126.      Move back one character.
  127.  
  128. C-f
  129.      Move forward one character.
  130.  
  131. DEL
  132.      Delete the character to the left of the cursor.
  133.  
  134. C-d
  135.      Delete the character underneath the cursor.
  136.  
  137. Printing characters
  138.      Insert itself into the line at the cursor.
  139.  
  140. C-_
  141.      Undo the last thing that you did.  You can undo all the way back
  142.      to an empty line.
  143.  
  144. File: readline.info,  Node: Readline Movement Commands,  Next: Readline Killing Commands,  Prev: Readline Bare Essentials,  Up: Readline Interaction
  145.  
  146. Readline Movement Commands
  147. --------------------------
  148.  
  149.    The above table describes the most basic possible keystrokes that
  150. you need in order to do editing of the input line.  For your
  151. convenience, many other commands have been added in addition to C-b,
  152. C-f, C-d, and DEL.  Here are some commands for moving more rapidly
  153. about the line.
  154.  
  155. C-a
  156.      Move to the start of the line.
  157.  
  158. C-e
  159.      Move to the end of the line.
  160.  
  161. M-f
  162.      Move forward a word.
  163.  
  164. M-b
  165.      Move backward a word.
  166.  
  167. C-l
  168.      Clear the screen, reprinting the current line at the top.
  169.  
  170.    Notice how C-f moves forward a character, while M-f moves forward a
  171. word.  It is a loose convention that control keystrokes operate on
  172. characters while meta keystrokes operate on words.
  173.  
  174. File: readline.info,  Node: Readline Killing Commands,  Next: Readline Arguments,  Prev: Readline Movement Commands,  Up: Readline Interaction
  175.  
  176. Readline Killing Commands
  177. -------------------------
  178.  
  179.    "Killing" text means to delete the text from the line, but to save
  180. it away for later use, usually by "yanking" it back into the line.  If
  181. the description for a command says that it `kills' text, then you can
  182. be sure that you can get the text back in a different (or the same)
  183. place later.
  184.  
  185.    Here is the list of commands for killing text.
  186.  
  187. C-k
  188.      Kill the text from the current cursor position to the end of the
  189.      line.
  190.  
  191. M-d
  192.      Kill from the cursor to the end of the current word, or if between
  193.      words, to the end of the next word.
  194.  
  195. M-DEL
  196.      Kill from the cursor the start of the previous word, or if between
  197.      words, to the start of the previous word.
  198.  
  199. C-w
  200.      Kill from the cursor to the previous whitespace.  This is
  201.      different than M-DEL because the word boundaries differ.
  202.  
  203.    And, here is how to "yank" the text back into the line.  Yanking is
  204.  
  205. C-y
  206.      Yank the most recently killed text back into the buffer at the
  207.      cursor.
  208.  
  209. M-y
  210.      Rotate the kill-ring, and yank the new top.  You can only do this
  211.      if the prior command is C-y or M-y.
  212.  
  213.    When you use a kill command, the text is saved in a "kill-ring".
  214. Any number of consecutive kills save all of the killed text together, so
  215. that when you yank it back, you get it in one clean sweep.  The kill
  216. ring is not line specific; the text that you killed on a previously
  217. typed line is available to be yanked back later, when you are typing
  218. another line.
  219.  
  220. File: readline.info,  Node: Readline Arguments,  Prev: Readline Killing Commands,  Up: Readline Interaction
  221.  
  222. Readline Arguments
  223. ------------------
  224.  
  225.    You can pass numeric arguments to Readline commands.  Sometimes the
  226. argument acts as a repeat count, other times it is the sign of the
  227. argument that is significant.  If you pass a negative argument to a
  228. command which normally acts in a forward direction, that command will
  229. act in a backward direction.  For example, to kill text back to the
  230. start of the line, you might type M- C-k.
  231.  
  232.    The general way to pass numeric arguments to a command is to type
  233. meta digits before the command.  If the first `digit' you type is a
  234. minus sign (-), then the sign of the argument will be negative.  Once
  235. you have typed one meta digit to get the argument started, you can type
  236. the remainder of the digits, and then the command.  For example, to give
  237. the C-d command an argument of 10, you could type M-1 0 C-d.
  238.  
  239. File: readline.info,  Node: Readline Init File,  Prev: Readline Interaction,  Up: Command Line Editing
  240.  
  241. Readline Init File
  242. ==================
  243.  
  244.    Although the Readline library comes with a set of Emacs-like
  245. keybindings, it is possible that you would like to use a different set
  246. of keybindings.  You can customize programs that use Readline by putting
  247. commands in an "init" file in your home directory.  The name of this
  248. file is `~/.inputrc'.
  249.  
  250.    When a program which uses the Readline library starts up, the
  251. `~/.inputrc' file is read, and the keybindings are set.
  252.  
  253.    In addition, the `C-x C-r' command re-reads this init file, thus
  254. incorporating any changes that you might have made to it.
  255.  
  256. * Menu:
  257.  
  258. * Readline Init Syntax::    Syntax for the commands in `~/.inputrc'.
  259. * Readline Vi Mode::        Switching to `vi' mode in Readline.
  260.  
  261. File: readline.info,  Node: Readline Init Syntax,  Next: Readline Vi Mode,  Up: Readline Init File
  262.  
  263. Readline Init Syntax
  264. --------------------
  265.  
  266.    There are only four constructs allowed in the `~/.inputrc' file:
  267.  
  268. Variable Settings
  269.      You can change the state of a few variables in Readline.  You do
  270.      this by using the `set' command within the init file.  Here is how
  271.      you would specify that you wish to use Vi line editing commands:
  272.  
  273.           set editing-mode vi
  274.  
  275.      Right now, there are only a few variables which can be set; so few
  276.      in fact, that we just iterate them here:
  277.  
  278.     `editing-mode'
  279.           The `editing-mode' variable controls which editing mode you
  280.           are using.  By default, GNU Readline starts up in Emacs
  281.           editing mode, where the keystrokes are most similar to Emacs.
  282.           This variable can either be set to `emacs' or `vi'.
  283.  
  284.     `horizontal-scroll-mode'
  285.           This variable can either be set to `On' or `Off'.  Setting it
  286.           to `On' means that the text of the lines that you edit will
  287.           scroll horizontally on a single screen line when they are
  288.           larger than the width of the screen, instead of wrapping onto
  289.           a new screen line.  By default, this variable is set to `Off'.
  290.  
  291.     `mark-modified-lines'
  292.           This variable when set to `On', says to display an asterisk
  293.           (`*') at the starts of history lines which have been modified.
  294.           This variable is off by default.
  295.  
  296.     `prefer-visible-bell'
  297.           If this variable is set to `On' it means to use a visible
  298.           bell if one is available, rather than simply ringing the
  299.           terminal bell.  By default, the value is `Off'.
  300.  
  301. Key Bindings
  302.      The syntax for controlling keybindings in the `~/.inputrc' file is
  303.      simple.  First you have to know the name of the command that you
  304.      want to change.  The following pages contain tables of the command
  305.      name, the default keybinding, and a short description of what the
  306.      command does.
  307.  
  308.      Once you know the name of the command, simply place the name of
  309.      the key you wish to bind the command to, a colon, and then the
  310.      name of the command on a line in the `~/.inputrc' file.  The name
  311.      of the key can be expressed in different ways, depending on which
  312.      is most comfortable for you.
  313.  
  314.     KEYNAME: FUNCTION-NAME or MACRO
  315.           KEYNAME is the name of a key spelled out in English.  For
  316.           example:
  317.                Control-u: universal-argument
  318.                Meta-Rubout: backward-kill-word
  319.                Control-o: ">&output"
  320.  
  321.           In the above example, `C-u' is bound to the function
  322.           `universal-argument', and `C-o' is bound to run the macro
  323.           expressed on the right hand side (that is, to insert the text
  324.           `>&output' into the line).
  325.  
  326.     "KEYSEQ": FUNCTION-NAME or MACRO
  327.           KEYSEQ differs from KEYNAME above in that strings denoting an
  328.           entire key sequence can be specified.  Simply place the key
  329.           sequence in double quotes.  GNU Emacs style key escapes can
  330.           be used, as in the following example:
  331.  
  332.                "\C-u": universal-argument
  333.                "\C-x\C-r": re-read-init-file
  334.                "\e[11~": "Function Key 1"
  335.  
  336.           In the above example, `C-u' is bound to the function
  337.           `universal-argument' (just as it was in the first example),
  338.           `C-x C-r' is bound to the function `re-read-init-file', and
  339.           `ESC [ 1 1 ~' is bound to insert the text `Function Key 1'.
  340.  
  341. * Menu:
  342.  
  343. * Commands For Moving::        Moving about the line.
  344. * Commands For History::    Getting at previous lines.
  345. * Commands For Text::        Commands for changing text.
  346. * Commands For Killing::    Commands for killing and yanking.
  347. * Numeric Arguments::        Specifying numeric arguments, repeat counts.
  348. * Commands For Completion::    Getting Readline to do the typing for you.
  349. * Miscellaneous Commands::    Other miscillaneous commands.
  350.  
  351. File: readline.info,  Node: Commands For Moving,  Next: Commands For History,  Up: Readline Init Syntax
  352.  
  353. Commands For Moving
  354. ...................
  355.  
  356. `beginning-of-line (C-a)'
  357.      Move to the start of the current line.
  358.  
  359. `end-of-line (C-e)'
  360.      Move to the end of the line.
  361.  
  362. `forward-char (C-f)'
  363.      Move forward a character.
  364.  
  365. `backward-char (C-b)'
  366.      Move back a character.
  367.  
  368. `forward-word (M-f)'
  369.      Move forward to the end of the next word.
  370.  
  371. `backward-word (M-b)'
  372.      Move back to the start of this, or the previous, word.
  373.  
  374. `clear-screen (C-l)'
  375.      Clear the screen leaving the current line at the top of the screen.
  376.  
  377. File: readline.info,  Node: Commands For History,  Next: Commands For Text,  Prev: Commands For Moving,  Up: Readline Init Syntax
  378.  
  379. Commands For Manipulating The History
  380. .....................................
  381.  
  382. `accept-line (Newline, Return)'
  383.      Accept the line regardless of where the cursor is.  If this line is
  384.      non-empty, add it to the history list.  If this line was a history
  385.      line, then restore the history line to its original state.
  386.  
  387. `previous-history (C-p)'
  388.      Move `up' through the history list.
  389.  
  390. `next-history (C-n)'
  391.      Move `down' through the history list.
  392.  
  393. `beginning-of-history (M-<)'
  394.      Move to the first line in the history.
  395.  
  396. `end-of-history (M->)'
  397.      Move to the end of the input history, i.e., the line you are
  398.      entering!
  399.  
  400. `reverse-search-history (C-r)'
  401.      Search backward starting at the current line and moving `up'
  402.      through the history as necessary.  This is an incremental search.
  403.  
  404. `forward-search-history (C-s)'
  405.      Search forward starting at the current line and moving `down'
  406.      through the the history as neccessary.
  407.  
  408. File: readline.info,  Node: Commands For Text,  Next: Commands For Killing,  Prev: Commands For History,  Up: Readline Init Syntax
  409.  
  410. Commands For Changing Text
  411. ..........................
  412.  
  413. `delete-char (C-d)'
  414.      Delete the character under the cursor.  If the cursor is at the
  415.      beginning of the line, and there are no characters in the line, and
  416.      the last character typed was not C-d, then return EOF.
  417.  
  418. `backward-delete-char (Rubout)'
  419.      Delete the character behind the cursor.  A numeric arg says to kill
  420.      the characters instead of deleting them.
  421.  
  422. `quoted-insert (C-q, C-v)'
  423.      Add the next character that you type to the line verbatim.  This is
  424.      how to insert things like C-q for example.
  425.  
  426. `tab-insert (M-TAB)'
  427.      Insert a tab character.
  428.  
  429. `self-insert (a, b, A, 1, !, ...)'
  430.      Insert yourself.
  431.  
  432. `transpose-chars (C-t)'
  433.      Drag the character before point forward over the character at
  434.      point.  Point moves forward as well.  If point is at the end of
  435.      the line, then transpose the two characters before point.
  436.      Negative args don't work.
  437.  
  438. `transpose-words (M-t)'
  439.      Drag the word behind the cursor past the word in front of the
  440.      cursor moving the cursor over that word as well.
  441.  
  442. `upcase-word (M-u)'
  443.      Uppercase the current (or following) word.  With a negative
  444.      argument, do the previous word, but do not move point.
  445.  
  446. `downcase-word (M-l)'
  447.      Lowercase the current (or following) word.  With a negative
  448.      argument, do the previous word, but do not move point.
  449.  
  450. `capitalize-word (M-c)'
  451.      Uppercase the current (or following) word.  With a negative
  452.      argument, do the previous word, but do not move point.
  453.  
  454. File: readline.info,  Node: Commands For Killing,  Next: Numeric Arguments,  Prev: Commands For Text,  Up: Readline Init Syntax
  455.  
  456. Killing And Yanking
  457. ...................
  458.  
  459. `kill-line (C-k)'
  460.      Kill the text from the current cursor position to the end of the
  461.      line.
  462.  
  463. `backward-kill-line ()'
  464.      Kill backward to the beginning of the line.  This is normally
  465.      unbound.
  466.  
  467. `kill-word (M-d)'
  468.      Kill from the cursor to the end of the current word, or if between
  469.      words, to the end of the next word.
  470.  
  471. `backward-kill-word (M-DEL)'
  472.      Kill the word behind the cursor.
  473.  
  474. `unix-line-discard (C-u)'
  475.      Do what C-u used to do in Unix line input.  We save the killed
  476.      text on the kill-ring, though.
  477.  
  478. `unix-word-rubout (C-w)'
  479.      Do what C-w used to do in Unix line input.  The killed text is
  480.      saved on the kill-ring.  This is different than backward-kill-word
  481.      because the word boundaries differ.
  482.  
  483. `yank (C-y)'
  484.      Yank the top of the kill ring into the buffer at point.
  485.  
  486. `yank-pop (M-y)'
  487.      Rotate the kill-ring, and yank the new top.  You can only do this
  488.      if the prior command is yank or yank-pop.
  489.  
  490. File: readline.info,  Node: Numeric Arguments,  Next: Commands For Completion,  Prev: Commands For Killing,  Up: Readline Init Syntax
  491.  
  492. Specifying Numeric Arguments
  493. ............................
  494.  
  495. `digit-argument (M-0, M-1, ... M--)'
  496.      Add this digit to the argument already accumulating, or start a new
  497.      argument.  M- starts a negative argument.
  498.  
  499. `universal-argument ()'
  500.      Do what C-u does in emacs.  By default, this is not bound.
  501.  
  502. File: readline.info,  Node: Commands For Completion,  Next: Miscellaneous Commands,  Prev: Numeric Arguments,  Up: Readline Init Syntax
  503.  
  504. Letting Readline Type For You
  505. .............................
  506.  
  507. `complete (TAB)'
  508.      Attempt to do completion on the text before point.  This is
  509.      implementation defined.  Generally, if you are typing a filename
  510.      argument, you can do filename completion; if you are typing a
  511.      command, you can do command completion, if you are typing in a
  512.      symbol to GDB, you can do symbol name completion, if you are
  513.      typing in a variable to Bash, you can do variable name
  514.      completion...
  515.  
  516. `possible-completions (M-?)'
  517.      List the possible completions of the text before point.
  518.  
  519. File: readline.info,  Node: Miscellaneous Commands,  Prev: Commands For Completion,  Up: Readline Init Syntax
  520.  
  521. Some Miscellaneous Commands
  522. ...........................
  523.  
  524. `re-read-init-file (C-x C-r)'
  525.      Read in the contents of your `~/.inputrc' file, and incorporate
  526.      any bindings found there.
  527.  
  528. `abort (C-g)'
  529.      Ding!  Stops things.
  530.  
  531. `do-uppercase-version (M-a, M-b, ...)'
  532.      Run the command that is bound to your uppercase brother.
  533.  
  534. `prefix-meta (ESC)'
  535.      Make the next character that you type be metafied.  This is for
  536.      people without a meta key.  Typing `ESC f' is equivalent to typing
  537.      `M-f'.
  538.  
  539. `undo (C-_)'
  540.      Incremental undo, separately remembered for each line.
  541.  
  542. `revert-line (M-r)'
  543.      Undo all changes made to this line.  This is like typing the `undo'
  544.      command enough times to get back to the beginning.
  545.  
  546. File: readline.info,  Node: Readline Vi Mode,  Prev: Readline Init Syntax,  Up: Readline Init File
  547.  
  548. Readline Vi Mode
  549. ----------------
  550.  
  551.    While the Readline library does not have a full set of Vi editing
  552. functions, it does contain enough to allow simple editing of the line.
  553.  
  554.    In order to switch interactively between Emacs and Vi editing modes,
  555. use the command M-C-j (toggle-editing-mode).
  556.  
  557.    When you enter a line in Vi mode, you are already placed in
  558. `insertion' mode, as if you had typed an `i'.  Pressing ESC switches
  559. you into `edit' mode, where you can edit the text of the line with the
  560. standard Vi movement keys, move to previous history lines with `k', and
  561. following lines with `j', and so forth.
  562.  
  563.    This document describes the GNU Readline Library, a utility for
  564. aiding in the consitency of user interface across discrete programs
  565. that need to provide a command line interface.
  566.  
  567.    Copyright (C) 1988 Free Software Foundation, Inc.
  568.  
  569.    Permission is granted to make and distribute verbatim copies of this
  570. manual provided the copyright notice and this permission notice pare
  571. preserved on all copies.
  572.  
  573.    Permission is granted to copy and distribute modified versions of
  574. this manual under the conditions for verbatim copying, provided that
  575. the entire resulting derived work is distributed under the terms of a
  576. permission notice identical to this one.
  577.  
  578.    Permission is granted to copy and distribute translations of this
  579. manual into another language, under the above conditions for modified
  580. versions, except that this permission notice may be stated in a
  581. translation approved by the Foundation.
  582.  
  583. File: readline.info,  Node: Programming with GNU Readline,  Next: Concept Index,  Prev: Command Line Editing,  Up: Top
  584.  
  585. Programming with GNU Readline
  586. *****************************
  587.  
  588.    This manual describes the interface between the GNU Readline Library
  589. and user programs.  If you are a programmer, and you wish to include the
  590. features found in GNU Readline in your own programs, such as completion,
  591. line editing, and interactive history manipulation, this documentation
  592. is for you.
  593.  
  594. * Menu:
  595.  
  596. * Default Behaviour::    Using the default behaviour of Readline.
  597. * Custom Functions::    Adding your own functions to Readline.
  598. * Custom Completers::    Supplanting or supplementing Readline's
  599.             completion functions.
  600.  
  601. File: readline.info,  Node: Default Behaviour,  Next: Custom Functions,  Up: Programming with GNU Readline
  602.  
  603. Default Behaviour
  604. =================
  605.  
  606.    Many programs provide a command line interface, such as `mail',
  607. `ftp', and `sh'.  For such programs, the default behaviour of Readline
  608. is sufficient.  This section describes how to use Readline in the
  609. simplest way possible, perhaps to replace calls in your code to `gets
  610. ()'.
  611.  
  612.    The function `readline' prints a prompt and then reads and returns a
  613. single line of text from the user.  The line which `readline ()'
  614. returns is allocated with `malloc ()'; you should `free ()' the line
  615. when you are done with it.  The declaration for `readline' in ANSI C is
  616.  
  617.      `char *readline (char *PROMPT);'
  618.  
  619.    So, one might say
  620.      `char *line = readline ("Enter a line: ");'
  621.    in order to read a line of text from the user.
  622.  
  623.    The line which is returned has the final newline removed, so only the
  624. text of the line remains.
  625.  
  626.    If readline encounters an `EOF' while reading the line, and the line
  627. is empty at that point, then `(char *)NULL' is returned.  Otherwise,
  628. the line is ended just as if a newline was typed.
  629.  
  630.    If you want the user to be able to get at the line later, (with C-p
  631. for example), you must call `add_history ()' to save the line away in a
  632. "history" list of such lines.
  633.  
  634.      `add_history (line)';
  635.  
  636.    For full details on the GNU History Library, see the associated
  637. manual.
  638.  
  639.    It is polite to avoid saving empty lines on the history list, since
  640. it is rare than someone has a burning need to reuse a blank line.  Here
  641. is a function which usefully replaces the standard `gets ()' library
  642. function:
  643.  
  644.      /* A static variable for holding the line. */
  645.      static char *line_read = (char *)NULL;
  646.      
  647.      /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  648.      char *
  649.      do_gets ()
  650.      {
  651.        /* If the buffer has already been allocated, return the memory
  652.           to the free pool. */
  653.        if (line_read != (char *)NULL)
  654.          {
  655.            free (line_read);
  656.            line_read = (char *)NULL;
  657.          }
  658.      
  659.        /* Get a line from the user. */
  660.        line_read = readline ("");
  661.      
  662.        /* If the line has any text in it, save it on the history. */
  663.        if (line_read && *line_read)
  664.          add_history (line_read);
  665.      
  666.        return (line_read);
  667.      }
  668.  
  669.    The above code gives the user the default behaviour of TAB
  670. completion: completion on file names.  If you do not want readline to
  671. complete on filenames, you can change the binding of the TAB key with
  672. `rl_bind_key ()'.
  673.  
  674.      `int rl_bind_key (int KEY, int (*FUNCTION)());'
  675.  
  676.    `rl_bind_key ()' takes 2 arguments; KEY is the character that you
  677. want to bind, and FUNCTION is the address of the function to run when
  678. KEY is pressed.  Binding TAB to `rl_insert ()' makes TAB just insert
  679. itself.
  680.  
  681.    `rl_bind_key ()' returns non-zero if KEY is not a valid ASCII
  682. character code (between 0 and 255).
  683.  
  684.      `rl_bind_key ('\t', rl_insert);'
  685.  
  686.    This code should be executed once at the start of your program; you
  687. might write a function called `initialize_readline ()' which performs
  688. this and other desired initializations, such as installing custom
  689. completers, etc.
  690.  
  691. File: readline.info,  Node: Custom Functions,  Next: Custom Completers,  Prev: Default Behaviour,  Up: Programming with GNU Readline
  692.  
  693. Custom Functions
  694. ================
  695.  
  696.    Readline provides a great many functions for manipulating the text of
  697. the line.  But it isn't possible to anticipate the needs of all
  698. programs.  This section describes the various functions and variables
  699. defined in within the Readline library which allow a user program to add
  700. customized functionality to Readline.
  701.  
  702. * Menu:
  703.  
  704. * The Function Type::    C declarations to make code readable.
  705. * Function Naming::    How to give a function you write a name.
  706. * Keymaps::        Making keymaps.
  707. * Binding Keys::    Changing Keymaps.
  708. * Function Writing::    Variables and calling conventions.
  709. * Allowing Undoing::    How to make your functions undoable.
  710.  
  711. File: readline.info,  Node: The Function Type,  Next: Function Naming,  Up: Custom Functions
  712.  
  713. The Function Type
  714. -----------------
  715.  
  716.    For the sake of readabilty, we declare a new type of object, called
  717. "Function".  A `Function' is a C language function which returns an
  718. `int'.  The type declaration for `Function' is:
  719.  
  720. `typedef int Function ();'
  721.  
  722.    The reason for declaring this new type is to make it easier to write
  723. code describing pointers to C functions.  Let us say we had a variable
  724. called FUNC which was a pointer to a function.  Instead of the classic
  725. C declaration
  726.  
  727.    `int (*)()func;'
  728.  
  729.    we have
  730.  
  731.    `Function *func;'
  732.  
  733. File: readline.info,  Node: Function Naming,  Next: Keymaps,  Prev: The Function Type,  Up: Custom Functions
  734.  
  735. Naming a Function
  736. -----------------
  737.  
  738.    The user can dynamically change the bindings of keys while using
  739. Readline.  This is done by representing the function with a descriptive
  740. name.  The user is able to type the descriptive name when referring to
  741. the function.  Thus, in an init file, one might find
  742.  
  743.      Meta-Rubout:    backward-kill-word
  744.  
  745.    This binds the keystroke Meta-Rubout to the function *descriptively*
  746. named `backward-kill-word'.  You, as the programmer, should bind the
  747. functions you write to descriptive names as well.  Readline provides a
  748. function for doing that:
  749.  
  750.  - Function: rl_add_defun (CHAR *NAME, FUNCTION *FUNCTION, INT KEY)
  751.      Add NAME to the list of named functions.  Make FUNCTION be the
  752.      function that gets called.  If KEY is not -1, then bind it to
  753.      FUNCTION using `rl_bind_key ()'.
  754.  
  755.    Using this function alone is sufficient for most applications.  It is
  756. the recommended way to add a few functions to the default functions that
  757. Readline has built in already.  If you need to do more or different
  758. things than adding a function to Readline, you may need to use the
  759. underlying functions described below.
  760.  
  761. File: readline.info,  Node: Keymaps,  Next: Binding Keys,  Prev: Function Naming,  Up: Custom Functions
  762.  
  763. Selecting a Keymap
  764. ------------------
  765.  
  766.    Key bindings take place on a "keymap".  The keymap is the
  767. association between the keys that the user types and the functions that
  768. get run.  You can make your own keymaps, copy existing keymaps, and tell
  769. Readline which keymap to use.
  770.  
  771.  - Function: Keymap rl_make_bare_keymap ()
  772.      Returns a new, empty keymap.  The space for the keymap is
  773.      allocated with `malloc ()'; you should `free ()' it when you are
  774.      done.
  775.  
  776.  - Function: Keymap rl_copy_keymap (KEYMAP MAP)
  777.      Return a new keymap which is a copy of MAP.
  778.  
  779.  - Function: Keymap rl_make_keymap ()
  780.      Return a new keymap with the printing characters bound to
  781.      rl_insert, the lowercase Meta characters bound to run their
  782.      equivalents, and the Meta digits bound to produce numeric
  783.      arguments.
  784.  
  785. File: readline.info,  Node: Binding Keys,  Next: Function Writing,  Prev: Keymaps,  Up: Custom Functions
  786.  
  787. Binding Keys
  788. ------------
  789.  
  790.    You associate keys with functions through the keymap.  Here are
  791. functions for doing that.
  792.  
  793.  - Function: int rl_bind_key (INT KEY, FUNCTION *FUNCTION)
  794.      Binds KEY to FUNCTION in the currently selected keymap.  Returns
  795.      non-zero in the case of an invalid KEY.
  796.  
  797.  - Function: int rl_bind_key_in_map (INT KEY, FUNCTION *FUNCTION,
  798.           KEYMAP MAP)
  799.      Bind KEY to FUNCTION in MAP.  Returns non-zero in the case of an
  800.      invalid KEY.
  801.  
  802.  - Function: int rl_unbind_key (INT KEY)
  803.      Make KEY do nothing in the currently selected keymap.  Returns
  804.      non-zero in case of error.
  805.  
  806.  - Function: int rl_unbind_key_in_map (INT KEY, KEYMAP MAP)
  807.      Make KEY be bound to the null function in MAP.  Returns non-zero
  808.      in case of error.
  809.  
  810.  - Function: rl_generic_bind (INT TYPE, CHAR *KEYSEQ, CHAR *DATA,
  811.           KEYMAP MAP)
  812.      Bind the key sequence represented by the string KEYSEQ to the
  813.      arbitrary pointer DATA.  TYPE says what kind of data is pointed to
  814.      by DATA; right now this can be a function (`ISFUNC'), a macro
  815.      (`ISMACR'), or a keymap (`ISKMAP').  This makes new keymaps as
  816.      necessary.  The initial place to do bindings is in MAP.
  817.  
  818. File: readline.info,  Node: Function Writing,  Next: Allowing Undoing,  Prev: Binding Keys,  Up: Custom Functions
  819.  
  820. Writing a New Function
  821. ----------------------
  822.  
  823.    In order to write new functions for Readline, you need to know the
  824. calling conventions for keyboard invoked functions, and the names of the
  825. variables that describe the current state of the line gathered so far.
  826.  
  827.  - Variable: char *rl_line_buffer
  828.      This is the line gathered so far.  You are welcome to modify the
  829.      contents of this, but see Undoing, below.
  830.  
  831.  - Variable: int rl_point
  832.      The offset of the current cursor position in RL_LINE_BUFFER.
  833.  
  834.  - Variable: int rl_end
  835.      The number of characters present in `rl_line_buffer'.  When
  836.      `rl_point' is at the end of the line, then `rl_point' and `rl_end'
  837.      are equal.
  838.  
  839.    The calling sequence for a command `foo' looks like
  840.  
  841.      `foo (int count, int key)'
  842.  
  843.    where COUNT is the numeric argument (or 1 if defaulted) and KEY is
  844. the key that invoked this function.
  845.  
  846.    It is completely up to the function as to what should be done with
  847. the numeric argument; some functions use it as a repeat count, other
  848. functions as a flag, and some choose to ignore it.  In general, if a
  849. function uses the numeric argument as a repeat count, it should be able
  850. to do something useful with a negative argument as well as a positive
  851. argument.  At the very least, it should be aware that it can be passed a
  852. negative argument.
  853.  
  854. File: readline.info,  Node: Allowing Undoing,  Prev: Function Writing,  Up: Custom Functions
  855.  
  856. Allowing Undoing
  857. ----------------
  858.  
  859.    Supporting the undo command is a painless thing to do, and makes your
  860. functions much more useful to the end user.  It is certainly easy to try
  861. something if you know you can undo it.  I could use an undo function for
  862. the stock market.
  863.  
  864.    If your function simply inserts text once, or deletes text once, and
  865. it calls `rl_insert_text ()' or `rl_delete_text ()' to do it, then
  866. undoing is already done for you automatically, and you can safely skip
  867. this section.
  868.  
  869.    If you do multiple insertions or multiple deletions, or any
  870. combination of these operations, you should group them together into
  871. one operation.  This can be done with `rl_begin_undo_group ()' and
  872. `rl_end_undo_group ()'.
  873.  
  874.  - Function: rl_begin_undo_group ()
  875.      Begins saving undo information in a group construct.  The undo
  876.      information usually comes from calls to `rl_insert_text ()' and
  877.      `rl_delete_text ()', but they could be direct calls to
  878.      `rl_add_undo ()'.
  879.  
  880.  - Function: rl_end_undo_group ()
  881.      Closes the current undo group started with `rl_begin_undo_group
  882.      ()'.  There should be exactly one call to `rl_end_undo_group ()'
  883.      for every call to `rl_begin_undo_group ()'.
  884.  
  885.    Finally, if you neither insert nor delete text, but directly modify
  886. the existing text (e.g. change its case), you call `rl_modifying ()'
  887. once, just before you modify the text.  You must supply the indices of
  888. the text range that you are going to modify.
  889.  
  890.  - Function: rl_modifying (INT START, INT END)
  891.      Tell Readline to save the text between START and END as a single
  892.      undo unit.  It is assumed that subsequent to this call you will
  893.      modify that range of text in some way.
  894.  
  895. An Example
  896. ----------
  897.  
  898.    Here is a function which changes lowercase characters to the
  899. uppercase equivalents, and uppercase characters to the lowercase
  900. equivalents.  If this function was bound to `M-c', then typing `M-c'
  901. would change the case of the character under point.  Typing `10 M-c'
  902. would change the case of the following 10 characters, leaving the
  903. cursor on the last character changed.
  904.  
  905.      /* Invert the case of the COUNT following characters. */
  906.      invert_case_line (count, key)
  907.           int count, key;
  908.      {
  909.        register int start, end;
  910.      
  911.        start = rl_point;
  912.      
  913.        if (count < 0)
  914.          {
  915.            direction = -1;
  916.            count = -count;
  917.          }
  918.        else
  919.          direction = 1;
  920.      
  921.        /* Find the end of the range to modify. */
  922.        end = start + (count * direction);
  923.      
  924.        /* Force it to be within range. */
  925.        if (end > rl_end)
  926.          end = rl_end;
  927.        else if (end < 0)
  928.          end = -1;
  929.      
  930.        if (start > end)
  931.          {
  932.            int temp = start;
  933.            start = end;
  934.            end = temp;
  935.          }
  936.      
  937.        if (start == end)
  938.          return;
  939.      
  940.        /* Tell readline that we are modifying the line, so save the undo
  941.           information. */
  942.        rl_modifying (start, end);
  943.      
  944.        for (; start != end; start += direction)
  945.          {
  946.            if (uppercase_p (rl_line_buffer[start]))
  947.              rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
  948.            else if (lowercase_p (rl_line_buffer[start]))
  949.              rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
  950.          }
  951.        /* Move point to on top of the last character changed. */
  952.        rl_point = end - direction;
  953.      }
  954.  
  955. File: readline.info,  Node: Custom Completers,  Prev: Custom Functions,  Up: Programming with GNU Readline
  956.  
  957. Custom Completers
  958. =================
  959.  
  960.    Typically, a program that reads commands from the user has a way of
  961. disambiguating commands and data.  If your program is one of these, then
  962. it can provide completion for either commands, or data, or both commands
  963. and data.  The following sections describe how your program and Readline
  964. cooperate to provide this service to end users.
  965.  
  966. * Menu:
  967.  
  968. * How Completing Works::    The logic used to do completion.
  969. * Completion Functions::    Functions provided by Readline.
  970. * Completion Variables::    Variables which control completion.
  971. * A Short Completion Example::    An example of writing completer subroutines.
  972.  
  973. File: readline.info,  Node: How Completing Works,  Next: Completion Functions,  Up: Custom Completers
  974.  
  975. How Completing Works
  976. --------------------
  977.  
  978.    In order to complete some text, the full list of possible completions
  979. must be available.  That is to say, it is not possible to accurately
  980. expand a partial word without knowing what all of the possible words
  981. that make sense in that context are.  The GNU Readline library provides
  982. the user interface to completion, and additionally, two of the most
  983. common completion functions; filename and username.  For completing
  984. other types of text, you must write your own completion function.  This
  985. section describes exactly what those functions must do, and provides an
  986. example function.
  987.  
  988.    There are three major functions used to perform completion:
  989.  
  990.   1. The user-interface function `rl_complete ()'.  This function is
  991.      called interactively with the same calling conventions as other
  992.      functions in readline intended for interactive use; i.e. COUNT,
  993.      and INVOKING-KEY.  It isolates the word to be completed and calls
  994.      `completion_matches ()' to generate a list of possible completions.
  995.      It then either lists the possible completions or actually performs
  996.      the completion, depending on which behaviour is desired.
  997.  
  998.   2. The internal function `completion_matches ()' uses your
  999.      "generator" function to generate the list of possible matches, and
  1000.      then returns the array of these matches.  You should place the
  1001.      address of your generator function in
  1002.      `rl_completion_entry_function'.
  1003.  
  1004.   3. The generator function is called repeatedly from
  1005.      `completion_matches ()', returning a string each time.  The
  1006.      arguments to the generator function are TEXT and STATE.  TEXT is
  1007.      the partial word to be completed.  STATE is zero the first time
  1008.      the function is called, and a positive non-zero integer for each
  1009.      subsequent call.  When the generator function returns `(char
  1010.      *)NULL' this signals `completion_matches ()' that there are no more
  1011.      possibilities left.
  1012.  
  1013.  
  1014.  - Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  1015.      Complete the word at or before point.  You have supplied the
  1016.      function that does the initial simple matching selection algorithm
  1017.      (see `completion_matches ()').  The default is to do filename
  1018.      completion.
  1019.  
  1020.    Note that `rl_complete ()' has the identical calling conventions as
  1021. any other key-invokable function; this is because by default it is bound
  1022. to the `TAB' key.
  1023.  
  1024.  - Variable: Function *rl_completion_entry_function
  1025.      This is a pointer to the generator function for `completion_matches
  1026.      ()'.  If the value of `rl_completion_entry_function' is `(Function
  1027.      *)NULL' then the default filename generator function is used,
  1028.      namely `filename_entry_function ()'.
  1029.  
  1030. File: readline.info,  Node: Completion Functions,  Next: Completion Variables,  Prev: How Completing Works,  Up: Custom Completers
  1031.  
  1032. Completion Functions
  1033. --------------------
  1034.  
  1035.    Here is the complete list of callable completion functions present in
  1036. Readline.
  1037.  
  1038.  - Function: rl_complete_internal (INT WHAT_TO_DO)
  1039.      Complete the word at or before point.  WHAT_TO_DO says what to do
  1040.      with the completion.  A value of `?' means list the possible
  1041.      completions.  `TAB' means do standard completion.  `*' means
  1042.      insert all of the possible completions.
  1043.  
  1044.  - Function: rl_complete (INT IGNORE, INT INVOKING_KEY)
  1045.      Complete the word at or before point.  You have supplied the
  1046.      function that does the initial simple matching selection algorithm
  1047.      (see `completion_matches ()').  The default is to do filename
  1048.      completion.  This just calls `rl_complete_internal ()' with an
  1049.      argument of `TAB'.
  1050.  
  1051.  - Function: rl_possible_completions ()
  1052.      List the possible completions.  See description of `rl_complete
  1053.      ()'.  This just calls `rl_complete_internal ()' with an argument of
  1054.      `?'.
  1055.  
  1056.  - Function: char **completion_matches (CHAR *TEXT, CHAR
  1057.           *(*ENTRY_FUNCTION) ())
  1058.      Returns an array of `(char *)' which is a list of completions for
  1059.      TEXT.  If there are no completions, returns `(char **)NULL'.  The
  1060.      first entry in the returned array is the substitution for TEXT.
  1061.      The remaining entries are the possible completions.  The array is
  1062.      terminated with a `NULL' pointer.
  1063.  
  1064.      ENTRY_FUNCTION is a function of two args, and returns a `(char
  1065.      *)'.  The first argument is TEXT.  The second is a state argument;
  1066.      it is zero on the first call, and non-zero on subsequent calls.
  1067.      It returns a `NULL'  pointer to the caller when there are no more
  1068.      matches.
  1069.  
  1070.  - Function: char *filename_completion_function (CHAR *TEXT, INT STATE)
  1071.      A generator function for filename completion in the general case.
  1072.      Note that completion in the Bash shell is a little different
  1073.      because of all the pathnames that must be followed when looking up
  1074.      the completion for a command.
  1075.  
  1076.  - Function: char *username_completion_function (CHAR *TEXT, INT STATE)
  1077.      A completion generator for usernames.  TEXT contains a partial
  1078.      username preceded by a random character (usually `~').
  1079.  
  1080. File: readline.info,  Node: Completion Variables,  Next: A Short Completion Example,  Prev: Completion Functions,  Up: Custom Completers
  1081.  
  1082. Completion Variables
  1083. --------------------
  1084.  
  1085.  - Variable: Function *rl_completion_entry_function
  1086.      A pointer to the generator function for `completion_matches ()'.
  1087.      `NULL' means to use `filename_entry_function ()', the default
  1088.      filename completer.
  1089.  
  1090.  - Variable: Function *rl_attempted_completion_function
  1091.      A pointer to an alternative function to create matches.  The
  1092.      function is called with TEXT, START, and END.  START and END are
  1093.      indices in `rl_line_buffer' saying what the boundaries of TEXT
  1094.      are.  If this function exists and returns `NULL' then `rl_complete
  1095.      ()' will call the value of `rl_completion_entry_function' to
  1096.      generate matches, otherwise the array of strings returned will be
  1097.      used.
  1098.  
  1099.  - Variable: int rl_completion_query_items
  1100.      Up to this many items will be displayed in response to a
  1101.      possible-completions call.  After that, we ask the user if she is
  1102.      sure she wants to see them all.  The default value is 100.
  1103.  
  1104.  - Variable: char *rl_basic_word_break_characters
  1105.      The basic list of characters that signal a break between words for
  1106.      the completer routine.  The contents of this variable is what
  1107.      breaks words in the Bash shell, i.e. " \t\n\"\\'`@$><=;|&{(".
  1108.  
  1109.  - Variable: char *rl_completer_word_break_characters
  1110.      The list of characters that signal a break between words for
  1111.      `rl_complete_internal ()'.  The default list is the contents of
  1112.      `rl_basic_word_break_characters'.
  1113.  
  1114.  - Variable: char *rl_special_prefixes
  1115.      The list of characters that are word break characters, but should
  1116.      be left in TEXT when it is passed to the completion function.
  1117.      Programs can use this to help determine what kind of completing to
  1118.      do.
  1119.  
  1120.  - Variable: int rl_ignore_completion_duplicates
  1121.      If non-zero, then disallow duplicates in the matches.  Default is
  1122.      1.
  1123.  
  1124.  - Variable: int rl_filename_completion_desired
  1125.      Non-zero means that the results of the matches are to be treated as
  1126.      filenames.  This is *always* zero on entry, and can only be changed
  1127.      within a completion entry generator function.
  1128.  
  1129.  - Variable: Function *rl_ignore_some_completions_function
  1130.      This function, if defined, is called by the completer when real
  1131.      filename completion is done, after all the matching names have
  1132.      been generated.  It is passed a `NULL' terminated array of `(char
  1133.      *)' known as MATCHES in the code.  The 1st element (`matches[0]')
  1134.      is the maximal substring that is common to all matches. This
  1135.      function can re-arrange the list of matches as required, but each
  1136.      deleted element of the array must be `free()''d.
  1137.  
  1138. File: readline.info,  Node: A Short Completion Example,  Prev: Completion Variables,  Up: Custom Completers
  1139.  
  1140. A Short Completion Example
  1141. --------------------------
  1142.  
  1143.    Here is a small application demonstrating the use of the GNU Readline
  1144. library.  It is called `fileman', and the source code resides in
  1145. `readline/examples/fileman.c'.  This sample application provides
  1146. completion of command names, line editing features, and access to the
  1147. history list.
  1148.  
  1149.      /* fileman.c -- A tiny application which demonstrates how to use the
  1150.         GNU Readline library.  This application interactively allows users
  1151.         to manipulate files and their modes. */
  1152.      
  1153.      #include <stdio.h>
  1154.      #include <readline/readline.h>
  1155.      #include <readline/history.h>
  1156.      #include <sys/types.h>
  1157.      #include <sys/file.h>
  1158.      #include <sys/stat.h>
  1159.      #include <sys/errno.h>
  1160.      
  1161.      /* The names of functions that actually do the manipulation. */
  1162.      int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  1163.      int com_delete (), com_help (), com_cd (), com_quit ();
  1164.      
  1165.      /* A structure which contains information on the commands this program
  1166.         can understand. */
  1167.      
  1168.      typedef struct {
  1169.        char *name;                   /* User printable name of the function. */
  1170.        Function *func;               /* Function to call to do the job. */
  1171.        char *doc;                    /* Documentation for this function.  */
  1172.      } COMMAND;
  1173.      
  1174.      COMMAND commands[] = {
  1175.        { "cd", com_cd, "Change to directory DIR" },
  1176.        { "delete", com_delete, "Delete FILE" },
  1177.        { "help", com_help, "Display this text" },
  1178.        { "?", com_help, "Synonym for `help'" },
  1179.        { "list", com_list, "List files in DIR" },
  1180.        { "ls", com_list, "Synonym for `list'" },
  1181.        { "pwd", com_pwd, "Print the current working directory" },
  1182.        { "quit", com_quit, "Quit using Fileman" },
  1183.        { "rename", com_rename, "Rename FILE to NEWNAME" },
  1184.        { "stat", com_stat, "Print out statistics on FILE" },
  1185.        { "view", com_view, "View the contents of FILE" },
  1186.        { (char *)NULL, (Function *)NULL, (char *)NULL }
  1187.      };
  1188.      
  1189.      /* The name of this program, as taken from argv[0]. */
  1190.      char *progname;
  1191.      
  1192.      /* When non-zero, this global means the user is done using this program. */
  1193.      int done = 0;
  1194.      
  1195.      main (argc, argv)
  1196.           int argc;
  1197.           char **argv;
  1198.      {
  1199.        progname = argv[0];
  1200.      
  1201.        initialize_readline ();       /* Bind our completer. */
  1202.      
  1203.        /* Loop reading and executing lines until the user quits. */
  1204.        while (!done)
  1205.          {
  1206.            char *line;
  1207.      
  1208.            line = readline ("FileMan: ");
  1209.      
  1210.            if (!line)
  1211.              {
  1212.                done = 1;             /* Encountered EOF at top level. */
  1213.              }
  1214.            else
  1215.              {
  1216.                /* Remove leading and trailing whitespace from the line.
  1217.                   Then, if there is anything left, add it to the history list
  1218.                   and execute it. */
  1219.                stripwhite (line);
  1220.      
  1221.                if (*line)
  1222.                  {
  1223.                    add_history (line);
  1224.                    execute_line (line);
  1225.                  }
  1226.              }
  1227.      
  1228.            if (line)
  1229.              free (line);
  1230.          }
  1231.        exit (0);
  1232.      }
  1233.      
  1234.      /* Execute a command line. */
  1235.      execute_line (line)
  1236.           char *line;
  1237.      {
  1238.        register int i;
  1239.        COMMAND *find_command (), *command;
  1240.        char *word;
  1241.      
  1242.        /* Isolate the command word. */
  1243.        i = 0;
  1244.        while (line[i] && !whitespace (line[i]))
  1245.          i++;
  1246.      
  1247.        word = line;
  1248.      
  1249.        if (line[i])
  1250.          line[i++] = '\0';
  1251.      
  1252.        command = find_command (word);
  1253.      
  1254.        if (!command)
  1255.          {
  1256.            fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1257.            return;
  1258.          }
  1259.      
  1260.        /* Get argument to command, if any. */
  1261.        while (whitespace (line[i]))
  1262.          i++;
  1263.      
  1264.        word = line + i;
  1265.      
  1266.        /* Call the function. */
  1267.        (*(command->func)) (word);
  1268.      }
  1269.      
  1270.      /* Look up NAME as the name of a command, and return a pointer to that
  1271.         command.  Return a NULL pointer if NAME isn't a command name. */
  1272.      COMMAND *
  1273.      find_command (name)
  1274.           char *name;
  1275.      {
  1276.        register int i;
  1277.      
  1278.        for (i = 0; commands[i].name; i++)
  1279.          if (strcmp (name, commands[i].name) == 0)
  1280.            return (&commands[i]);
  1281.      
  1282.        return ((COMMAND *)NULL);
  1283.      }
  1284.      
  1285.      /* Strip whitespace from the start and end of STRING. */
  1286.      stripwhite (string)
  1287.           char *string;
  1288.      {
  1289.        register int i = 0;
  1290.      
  1291.        while (whitespace (string[i]))
  1292.          i++;
  1293.      
  1294.        if (i)
  1295.          strcpy (string, string + i);
  1296.      
  1297.        i = strlen (string) - 1;
  1298.      
  1299.        while (i > 0 && whitespace (string[i]))
  1300.          i--;
  1301.      
  1302.        string[++i] = '\0';
  1303.      }
  1304.      
  1305.      /* **************************************************************** */
  1306.      /*                                                                  */
  1307.      /*                  Interface to Readline Completion                */
  1308.      /*                                                                  */
  1309.      /* **************************************************************** */
  1310.      
  1311.      /* Tell the GNU Readline library how to complete.  We want to try to complete
  1312.         on command names if this is the first word in the line, or on filenames
  1313.         if not. */
  1314.      initialize_readline ()
  1315.      {
  1316.        char **fileman_completion ();
  1317.      
  1318.        /* Allow conditional parsing of the ~/.inputrc file. */
  1319.        rl_readline_name = "FileMan";
  1320.      
  1321.        /* Tell the completer that we want a crack first. */
  1322.        rl_attempted_completion_function = (Function *)fileman_completion;
  1323.      }
  1324.      
  1325.      /* Attempt to complete on the contents of TEXT.  START and END show the
  1326.         region of TEXT that contains the word to complete.  We can use the
  1327.         entire line in case we want to do some simple parsing.  Return the
  1328.         array of matches, or NULL if there aren't any. */
  1329.      char **
  1330.      fileman_completion (text, start, end)
  1331.           char *text;
  1332.           int start, end;
  1333.      {
  1334.        char **matches;
  1335.        char *command_generator ();
  1336.      
  1337.        matches = (char **)NULL;
  1338.      
  1339.        /* If this word is at the start of the line, then it is a command
  1340.           to complete.  Otherwise it is the name of a file in the current
  1341.           directory. */
  1342.        if (start == 0)
  1343.          matches = completion_matches (text, command_generator);
  1344.      
  1345.        return (matches);
  1346.      }
  1347.      
  1348.      /* Generator function for command completion.  STATE lets us know whether
  1349.         to start from scratch; without any state (i.e. STATE == 0), then we
  1350.         start at the top of the list. */
  1351.      char *
  1352.      command_generator (text, state)
  1353.           char *text;
  1354.           int state;
  1355.      {
  1356.        static int list_index, len;
  1357.        char *name;
  1358.      
  1359.        /* If this is a new word to complete, initialize now.  This includes
  1360.           saving the length of TEXT for efficiency, and initializing the index
  1361.           variable to 0. */
  1362.        if (!state)
  1363.          {
  1364.            list_index = 0;
  1365.            len = strlen (text);
  1366.          }
  1367.      
  1368.        /* Return the next name which partially matches from the command list. */
  1369.        while (name = commands[list_index].name)
  1370.          {
  1371.            list_index++;
  1372.      
  1373.            if (strncmp (name, text, len) == 0)
  1374.              return (name);
  1375.          }
  1376.      
  1377.        /* If no names matched, then return NULL. */
  1378.        return ((char *)NULL);
  1379.      }
  1380.      
  1381.      /* **************************************************************** */
  1382.      /*                                                                  */
  1383.      /*                       FileMan Commands                           */
  1384.      /*                                                                  */
  1385.      /* **************************************************************** */
  1386.      
  1387.      /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1388.         commands. */
  1389.      static char syscom[1024];
  1390.      
  1391.      /* List the file(s) named in arg. */
  1392.      com_list (arg)
  1393.           char *arg;
  1394.      {
  1395.        if (!arg)
  1396.          arg = "*";
  1397.      
  1398.        sprintf (syscom, "ls -FClg %s", arg);
  1399.        system (syscom);
  1400.      }
  1401.      
  1402.      com_view (arg)
  1403.           char *arg;
  1404.      {
  1405.        if (!valid_argument ("view", arg))
  1406.          return;
  1407.      
  1408.        sprintf (syscom, "cat %s | more", arg);
  1409.        system (syscom);
  1410.      }
  1411.      
  1412.      com_rename (arg)
  1413.           char *arg;
  1414.      {
  1415.        too_dangerous ("rename");
  1416.      }
  1417.      
  1418.      com_stat (arg)
  1419.           char *arg;
  1420.      {
  1421.        struct stat finfo;
  1422.      
  1423.        if (!valid_argument ("stat", arg))
  1424.          return;
  1425.      
  1426.        if (stat (arg, &finfo) == -1)
  1427.          {
  1428.            perror (arg);
  1429.            return;
  1430.          }
  1431.      
  1432.        printf ("Statistics for `%s':\n", arg);
  1433.      
  1434.        printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
  1435.                finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
  1436.        printf ("      Created on: %s", ctime (&finfo.st_ctime));
  1437.        printf ("  Last access at: %s", ctime (&finfo.st_atime));
  1438.        printf ("Last modified at: %s", ctime (&finfo.st_mtime));
  1439.      }
  1440.      
  1441.      com_delete (arg)
  1442.           char *arg;
  1443.      {
  1444.        too_dangerous ("delete");
  1445.      }
  1446.      
  1447.      /* Print out help for ARG, or for all of the commands if ARG is
  1448.         not present. */
  1449.      com_help (arg)
  1450.           char *arg;
  1451.      {
  1452.        register int i;
  1453.        int printed = 0;
  1454.      
  1455.        for (i = 0; commands[i].name; i++)
  1456.          {
  1457.            if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1458.              {
  1459.                printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1460.                printed++;
  1461.              }
  1462.          }
  1463.      
  1464.        if (!printed)
  1465.          {
  1466.            printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1467.      
  1468.            for (i = 0; commands[i].name; i++)
  1469.              {
  1470.                /* Print in six columns. */
  1471.                if (printed == 6)
  1472.                  {
  1473.                    printed = 0;
  1474.                    printf ("\n");
  1475.                  }
  1476.      
  1477.                printf ("%s\t", commands[i].name);
  1478.                printed++;
  1479.              }
  1480.      
  1481.            if (printed)
  1482.              printf ("\n");
  1483.          }
  1484.      }
  1485.      
  1486.      /* Change to the directory ARG. */
  1487.      com_cd (arg)
  1488.           char *arg;
  1489.      {
  1490.        if (chdir (arg) == -1)
  1491.          perror (arg);
  1492.      
  1493.        com_pwd ("");
  1494.      }
  1495.      
  1496.      /* Print out the current working directory. */
  1497.      com_pwd (ignore)
  1498.           char *ignore;
  1499.      {
  1500.        char dir[1024];
  1501.      
  1502.        (void) getwd (dir);
  1503.      
  1504.        printf ("Current directory is %s\n", dir);
  1505.      }
  1506.      
  1507.      /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1508.      com_quit (arg)
  1509.           char *arg;
  1510.      {
  1511.        done = 1;
  1512.      }
  1513.      
  1514.      /* Function which tells you that you can't do this. */
  1515.      too_dangerous (caller)
  1516.           char *caller;
  1517.      {
  1518.        fprintf (stderr,
  1519.                 "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1520.                 caller);
  1521.      }
  1522.      
  1523.      /* Return non-zero if ARG is a valid argument for CALLER, else print
  1524.         an error message and return zero. */
  1525.      int
  1526.      valid_argument (caller, arg)
  1527.           char *caller, *arg;
  1528.      {
  1529.        if (!arg || !*arg)
  1530.          {
  1531.            fprintf (stderr, "%s: Argument required.\n", caller);
  1532.            return (0);
  1533.          }
  1534.      
  1535.        return (1);
  1536.      }
  1537.  
  1538. File: readline.info,  Node: Concept Index,  Next: Function and Variable Index,  Prev: Programming with GNU Readline,  Up: Top
  1539.  
  1540. Concept Index
  1541. *************
  1542.  
  1543. * Menu:
  1544.  
  1545. * interaction, readline:                Readline Interaction.
  1546. * readline, function:                   Default Behaviour.
  1547.  
  1548. File: readline.info,  Node: Function and Variable Index,  Prev: Concept Index,  Up: Top
  1549.  
  1550. Function and Variable Index
  1551. ***************************
  1552.  
  1553. * Menu:
  1554.  
  1555. * abort (C-g):                          Miscellaneous Commands.
  1556. * accept-line (Newline, Return):        Commands For History.
  1557. * backward-char (C-b):                  Commands For Moving.
  1558. * backward-delete-char (Rubout):        Commands For Text.
  1559. * backward-kill-line ():                Commands For Killing.
  1560. * backward-kill-word (M-DEL):           Commands For Killing.
  1561. * backward-word (M-b):                  Commands For Moving.
  1562. * beginning-of-history (M-<):           Commands For History.
  1563. * beginning-of-line (C-a):              Commands For Moving.
  1564. * capitalize-word (M-c):                Commands For Text.
  1565. * char **completion_matches:            Completion Functions.
  1566. * char *filename_completion_function:   Completion Functions.
  1567. * char *rl_basic_word_break_characters: Completion Variables.
  1568. * char *rl_completer_word_break_characters: Completion Variables.
  1569. * char *rl_line_buffer:                 Function Writing.
  1570. * char *rl_special_prefixes:            Completion Variables.
  1571. * char *username_completion_function:   Completion Functions.
  1572. * clear-screen (C-l):                   Commands For Moving.
  1573. * complete (TAB):                       Commands For Completion.
  1574. * delete-char (C-d):                    Commands For Text.
  1575. * digit-argument (M-0, M-1, ... M-):    Numeric Arguments.
  1576. * do-uppercase-version (M-a, M-b, ...): Miscellaneous Commands.
  1577. * downcase-word (M-l):                  Commands For Text.
  1578. * editing-mode:                         Readline Init Syntax.
  1579. * end-of-history (M->):                 Commands For History.
  1580. * end-of-line (C-e):                    Commands For Moving.
  1581. * forward-char (C-f):                   Commands For Moving.
  1582. * forward-search-history (C-s):         Commands For History.
  1583. * forward-word (M-f):                   Commands For Moving.
  1584. * Function *rl_attempted_completion_function: Completion Variables.
  1585. * Function *rl_completion_entry_function: Completion Variables.
  1586. * Function *rl_completion_entry_function: How Completing Works.
  1587. * Function *rl_ignore_some_completions_function: Completion Variables.
  1588. * horizontal-scroll-mode:               Readline Init Syntax.
  1589. * int rl_bind_key:                      Binding Keys.
  1590. * int rl_bind_key_in_map:               Binding Keys.
  1591. * int rl_completion_query_items:        Completion Variables.
  1592. * int rl_end:                           Function Writing.
  1593. * int rl_filename_completion_desired:   Completion Variables.
  1594. * int rl_ignore_completion_duplicates:  Completion Variables.
  1595. * int rl_point:                         Function Writing.
  1596. * int rl_unbind_key:                    Binding Keys.
  1597. * int rl_unbind_key_in_map:             Binding Keys.
  1598. * Keymap rl_copy_keymap:                Keymaps.
  1599. * Keymap rl_make_bare_keymap:           Keymaps.
  1600. * Keymap rl_make_keymap:                Keymaps.
  1601. * kill-line (C-k):                      Commands For Killing.
  1602. * kill-word (M-d):                      Commands For Killing.
  1603. * mark-modified-lines:                  Readline Init Syntax.
  1604. * next-history (C-n):                   Commands For History.
  1605. * possible-completions (M-?):           Commands For Completion.
  1606. * prefer-visible-bell:                  Readline Init Syntax.
  1607. * prefix-meta (ESC):                    Miscellaneous Commands.
  1608. * previous-history (C-p):               Commands For History.
  1609. * quoted-insert (C-q, C-v):             Commands For Text.
  1610. * re-read-init-file (C-x C-r):          Miscellaneous Commands.
  1611. * readline ():                          Default Behaviour.
  1612. * reverse-search-history (C-r):         Commands For History.
  1613. * revert-line (M-r):                    Miscellaneous Commands.
  1614. * rl_add_defun:                         Function Naming.
  1615. * rl_begin_undo_group:                  Allowing Undoing.
  1616. * rl_bind_key ():                       Default Behaviour.
  1617. * rl_complete:                          Completion Functions.
  1618. * rl_complete:                          How Completing Works.
  1619. * rl_complete_internal:                 Completion Functions.
  1620. * rl_end_undo_group:                    Allowing Undoing.
  1621. * rl_generic_bind:                      Binding Keys.
  1622. * rl_modifying:                         Allowing Undoing.
  1623. * rl_possible_completions:              Completion Functions.
  1624. * self-insert (a, b, A, 1, !, ...):     Commands For Text.
  1625. * tab-insert (M-TAB):                   Commands For Text.
  1626. * transpose-chars (C-t):                Commands For Text.
  1627. * transpose-words (M-t):                Commands For Text.
  1628. * undo (C-_):                           Miscellaneous Commands.
  1629. * universal-argument ():                Numeric Arguments.
  1630. * unix-line-discard (C-u):              Commands For Killing.
  1631. * unix-word-rubout (C-w):               Commands For Killing.
  1632. * upcase-word (M-u):                    Commands For Text.
  1633. * yank (C-y):                           Commands For Killing.
  1634. * yank-pop (M-y):                       Commands For Killing.
  1635.  
  1636.  
  1637. Tag Table:
  1638. Node: Top1000
  1639. Node: Command Line Editing1613
  1640. Node: Introduction and Notation2036
  1641. Node: Readline Interaction3058
  1642. Node: Readline Bare Essentials4197
  1643. Node: Readline Movement Commands5705
  1644. Node: Readline Killing Commands6596
  1645. Node: Readline Arguments8236
  1646. Node: Readline Init File9187
  1647. Node: Readline Init Syntax10015
  1648. Node: Commands For Moving13962
  1649. Node: Commands For History14587
  1650. Node: Commands For Text15662
  1651. Node: Commands For Killing17329
  1652. Node: Numeric Arguments18456
  1653. Node: Commands For Completion18899
  1654. Node: Miscellaneous Commands19623
  1655. Node: Readline Vi Mode20465
  1656. Node: Programming with GNU Readline22075
  1657. Node: Default Behaviour22780
  1658. Node: Custom Functions26001
  1659. Node: The Function Type26800
  1660. Node: Function Naming27433
  1661. Node: Keymaps28685
  1662. Node: Binding Keys29600
  1663. Node: Function Writing30901
  1664. Node: Allowing Undoing32342
  1665. Node: Custom Completers35844
  1666. Node: How Completing Works36592
  1667. Node: Completion Functions39397
  1668. Node: Completion Variables41730
  1669. Node: A Short Completion Example44500
  1670. Node: Concept Index56126
  1671. Node: Function and Variable Index56415
  1672. End Tag Table
  1673.